﻿$(function () {
    $("input.withprompt").focus(function () {
        if (this.value == this.defaultValue) {
            this.value = '';
            this.style.color = '#333333';
        }
    });

    $("input.withprompt").blur(function () {
        if (this.value == '') {
            this.value = this.defaultValue;
            this.style.color = '#b8b8b8';
        }
    });


    $("input.withprompt").change(function () {
        if (this.value == this.defaultValue) {
            this.style.color = '#b8b8b8';
        }
        else {
            this.style.color = '#333333';
        }
    });

    $.extend($.fn.datagrid.methods, {
        getChecked: function (jq) {
            var rr = [];
            var rows = jq.datagrid('getRows');
            jq.datagrid('getPanel').find('div.datagrid-cell-check input:checked').each(function () {
                var index = $(this).parents('tr:first').attr('datagrid-row-index');
                rr.push(rows[index]);
            });
            return rr;
        }
    });

    $.extend($.fn.datagrid.methods, {
        deleteChecked: function (jq) {
            var rows = jq.datagrid('getRows');
            jq.datagrid('getPanel').find('div.datagrid-cell-check input:checked').each(function () {
                var index = $(this).parents('tr:first').attr('datagrid-row-index');
                jq.datagrid("deleteRow", index);
            });
        }
    });

    (function ($) {
        jQuery.fn.serializeJSON = function () {
            var json = {};
            jQuery.map(jQuery(this).serializeArray(), function (n, i) {
                var _ = n.name.indexOf('[');
                if (_ > -1) {
                    var o = json;
                    _name = n.name.replace(/\]/gi, '').split('[');
                    for (var i = 0, len = _name.length; i < len; i++) {
                        if (i == len - 1) {
                            if (o[_name[i]]) {
                                if (typeof o[_name[i]] == 'string') {
                                    o[_name[i]] = [o[_name[i]]];
                                }
                                o[_name[i]].push(n.value);
                            }
                            else o[_name[i]] = n.value || '';
                        }
                        else o = o[_name[i]] = o[_name[i]] || {};
                    }
                }
                else {
                    if (json[n.name] !== undefined) {
                        if (!json[n.name].push) {
                            json[n.name] = [json[n.name]];
                        }
                        json[n.name].push(n.value || '');
                    }
                    else json[n.name] = n.value || '';
                }
            });
            return json;
        };
    })(jQuery);
});